// FacebookServerSession.cs
// Facebook/FrameworkWeb/Web
// Copyright (c) 2007, Nikhil Kothari. All Rights Reserved.
//

using System;
using System.Diagnostics;
using System.Web;
using Facebook.Service.Core;

namespace Facebook.Web {

    /// <summary>
    /// Represents a Facebook session that is used within a Web
    /// application that serves requests coming from Facebook.
    /// </summary>
    public abstract class FacebookServerSession : FacebookSession {

        internal const string FacebookLoginUrl = "http://www.facebook.com/login.php?api_key={0}&v=1.0";
        internal const string FacebookAddAppUrl = "http://www.facebook.com/add.php?api_key={0}&v=1.0";

        private FacebookApplication _application;

        /// <summary>
        /// Creates an instance of a FacebookServerSession with the
        /// specified application information.
        /// </summary>
        /// <param name="application">The FacebookApplication control that owns this session.</param>
        /// <param name="appKey">The application key used as an API key.</param>
        /// <param name="secret">The application secret used to sign requests.</param>
        internal FacebookServerSession(FacebookApplication application, string appKey, string secret)
            : base(appKey, secret) {
            Debug.Assert(application != null);
            _application = application;
        }

        /// <summary>
        /// The current FacebookApplication control that is using this session.
        /// </summary>
        protected FacebookApplication Application {
            get {
                return _application;
            }
        }

        /// <summary>
        /// Initializes the FacebookServiceSession instance based on the
        /// current request to the server.
        /// </summary>
        /// <param name="context">Represents the current request being processed.</param>
        /// <returns>A URL to redirect to if a redirect needs to be performed; null otherwise.</returns>
        protected internal virtual string Initialize(HttpContext context) {
            HttpRequest request = context.Request;

            string[] friendIDs = null;

            string friendIDList = request["fb_sig_friends"];
            if (friendIDList != null) {
                if (friendIDList.Length == 0) {
                    friendIDs = new string[0];
                }
                else {
                    friendIDs = friendIDList.Split(',');
                }
            }
            InitializeFriends(friendIDs);

            return null;
        }

        /// <summary>
        /// Loads any state information preserved as part of the page.
        /// </summary>
        /// <param name="state">The previously preserved state.</param>
        protected internal virtual void LoadState(object state) {
        }

        /// <summary>
        /// Saves any relevant state information that is to be saved as part of the page.
        /// </summary>
        /// <returns>The saved state that needs to be preserved.</returns>
        protected internal virtual object SaveState() {
            return null;
        }
    }
}
